
var fs = require('fs');
var path = require('path');
var app = require('http').createServer(function(req, res){
	var directory = '/public/'
	var filePath = req.url;

	if(filePath == '/'){
		filePath = 'sylveo.html';
	}

	filePath = directory+filePath;

	var extname = path.extname(filePath); //Gestion des extensions de fichier et changement du type MIME
	var contentTypesByExtention = {
		'.html': 'text/html',
		'.js': 'text/javascript',
		'.css':  'text/css'
	};

	var contentType = contentTypesByExtention[extname] || 'text/plain';


	path.exists(__dirname + filePath, function(exists) {
		if (exists) {
			var html = fs.readFileSync(__dirname + filePath);
			res.writeHead(200, {
				'Content-Type': contentType
			});
			res.end(html, 'utf-8');
		}
		else {
			res.writeHead(404);
			res.end("Erreur 404");
		}
	});
});

var io = require('socket.io').listen(app, {
	log: false
});



//Console colors
var red, blue, reset;
red   = '\033[31m';
blue  = '\033[34m';
white = '\033[37m';
reset = '\033[0m';

//Port du serveur web
app.listen(8333);
console.log(red + '[Server]' + reset + ' Server start');





var currentState = 'openning';

var devices = {
	ipad: '',
	iphone: '',
	admin: ''
};


//Connection client
io.sockets.on('connection', function (client) {
	var sylveoClient = {
		obj: client,
		device: '',
		state: ''
	};

	//Notify client
	client.emit('deviceConnected', {
		connected: true
	});

	//Inititalize client
	client.on('setDevice', function(data){
		sylveoClient.device = data.device;

		if(sylveoClient.device != 'admin')
		{
			sylveoClient.state = data.state;
		}
		
		console.log(blue + '[Device] ' + reset + sylveoClient.device + ' connected ');

		if(sylveoClient.device == 'ipad')
		{
			devices.ipad = sylveoClient;
		}
		else if(sylveoClient.device == 'iphone')
		{
			devices.iphone = sylveoClient;
		}
		else if(sylveoClient.device == 'admin')
		{
			devices.admin = sylveoClient;
		}
	});

	//Change device state
	client.on('changeDeviceState', function(data){
		sylveoClient.state = data.newState;

		console.log(blue + '[Device] ' + reset + white + '[State] ' + reset + sylveoClient.device + ' changed is state to ' + sylveoClient.state);

		if(devices.ipad.state == devices.iphone.state)
		{
			currentState = devices.ipad.state;
			console.log(white + '[State] ' + reset + currentState);

			sylveoClient.obj.broadcast.emit('statesSynchronized',{
				state: currentState
			});

			sylveoClient.obj.emit('statesSynchronized',{
				state: currentState
			});
		}
	});

	//Change state
	client.on('changeState', function(data){
		currentState = data.newState;

		devices.ipad.state = currentState;
		devices.iphone.state = currentState;

		sylveoClient.obj.broadcast.emit('changeState',{
			changeState: currentState
		});

		console.log(white + '[State] ' + reset + currentState);
	});

	//Send Photo
	client.on('sendPhoto', function(data){
		sylveoClient.obj.broadcast.emit('sendPhoto',{
			photo: data.photo
		});

		console.log(white + '[Game] ' + reset + 'Sending picture');
	});

	client.on('validatePhoto', function(data){
		sylveoClient.obj.broadcast.emit('validatePhoto',{
			validate: data.validate
		});

		if(data.validate == true)
		{
			console.log(white + '[Game] ' + reset + 'Picture validated');
		}
		else
		{
			console.log(white + '[Game] ' + reset + 'Picture refused');
		}
	});

	//Quizz
	client.on('answerQuizz', function(data){
		sylveoClient.obj.broadcast.emit('answerQuizz',{
			response: data.response
		});

		console.log(white + '[Game] ' + reset + 'Sending response : ' + data.response);
	});

	client.on('validateQuizz', function(data){
		sylveoClient.obj.broadcast.emit('validateQuizz',{
			validate: data.validate
		});

		if(data.validate == true)
		{
			console.log(white + '[Game] ' + reset + 'Quizz validated');
		}
		else
		{
			console.log(white + '[Game] ' + reset + 'Quizz refused');
		}
	});

	//Send Photo Finish
	client.on('sendPhotoFinish', function(data){
		sylveoClient.obj.broadcast.emit('sendPhotoFinish',{
			photo: data.photo
		});

		console.log(white + '[PhotoFinish] ' + reset + 'Sending picture');
	});

	//Disconnection
	client.on('disconnect', function () {
		console.log(blue + '[Device] ' + reset + sylveoClient.device + ' disconnected');
	});
});